package cn.jdy.plugins.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.platform.service.ConfService;
import cn.jdy.plugins.entity.CRateOrder;
import cn.jdy.plugins.entity.Live;

/**
 * @title 旅居订单管理后台操作
 * @author jga
 * @since 1-12-2015
 */
@Service
public class CRateOrderService extends BaseDao<CRateOrder>{

	@Autowired
	private ConfService confService;
	
	/**
	 * 按条件查询
	 * @param sp
	 * 
	 * @return
	 */
	public List<CRateOrder> query(CRateOrder sp){
		if(sp==null) return super.findAll();
		if(sp.getiDisplayLength() > 0){
	     return super.find(createFilter(sp),sp.getiDisplayStart(),sp.getiDisplayLength());
			}
		return super.find(createFilter(sp));
	}
	 public List<CRateOrder> querypage(CRateOrder sp){
	       return super.find(createFilter(sp),sp.getPageNo(),18);
	  }
	public Long count(CRateOrder sp){
		return super.count(createFilter(sp));
	}
	/**
	 * 拼装条件
	 * @param sp
	 * 
	 * @return
	 */
	private DetachedCriteria createFilter(CRateOrder sp){
		DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
 
	  
    if(!StringUtil.isNullOrEmpty(sp.getCreateTime())&&!",".equals(sp.getCreateTime())) {
   
        if( sp.getCreateTime().split(",").length>1){
          dc.add(Restrictions.between("createTime", sp.getCreateTime().split(",")[0], sp.getCreateTime().split(",")[1]));
        }else{
          dc.add(Restrictions.like("createTime", sp.getCreateTime(),MatchMode.ANYWHERE));
        }
     
    }
	  if(StringUtil.isNotEmpty(sp.getProductxid())&&sp.getProductxid().equals("80")){
		  dc.add(Restrictions.or(Restrictions.eq("productxid","89"),Restrictions.eq("productxid","90")));
	  }
	  if(StringUtil.isNotEmpty(sp.getProductxid())&&sp.getProductxid().equals("90")){
		  dc.add(Restrictions.ne("productxid","89"));
		  dc.add(Restrictions.ne("productxid","90"));
	  }
	  if(StringUtil.isNotEmpty(sp.getMemberid())){
		  dc.add(Restrictions.eq("memberid", sp.getMemberid()));
	  }
		dc.addOrder(Order.desc("createTime"));
			return dc;
	}
	
	
	/**
	   * web police
	   * 查询订单和商品的信息
	   * @return
	   */
	  public List<Map<String,Object>> cRateCountQuery(String userid) {
		    List<Map<String,Object>> list=new  ArrayList<Map<String,Object>>();
	    try {
	    	//查询 直播开始时间，直播结束时间，直播时长
	    	String sql = "select c.id id,c.createTime createTime ,mch_id,memberid,c.username username,orderNo,productxid,productxname,total,rzts,fs,lv,sylv,mobile,dqtime,istqsr from "
	    		+	" CRateOrder c, productx a, member m where c.memberid=m.id"
	    		+	" and c.productxid=a.id and c.zfstatus=1 "
	    		+	" and  c.sendStatus !=4 ";
	  
	    	      SQLQuery sqlQuery = em().createSQLQuery(sql);
	    	 // 将查询寻结果转换为Map格式，解决多表查询或零散字段查询 映射取值问题
	    	      sqlQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
				 list= sqlQuery.list();
		  
		    
		      return  list;
		    } catch (Exception e) {
		      BaseLog.e(this.getClass(), "查询异常！", e);
		    }
		    return list;
		  }
 /*
  * *查询是否已经购买新手单
  */
	  public int countorderMap(String id){
			int count=0;
			try {
				String sql="select count(c.id) from cRateOrder c,Member m,Productx p where "
						+ "c.memberid=m.id and p.id=c.productxid  and c.memberid=? ";
 
				Query query = super.em().createSQLQuery(sql);
				query.setParameter(0,id);
				count = Integer.parseInt(String.valueOf(query.uniqueResult()));
			} catch (Exception e) {
				BaseLog.e(this.getClass(), "查询异常", e);
			}
				
			return count;
		}

}
